home *** CD-ROM | disk | FTP | other *** search
-
- #ifndef _NSTRING_INLINES_H_
- #define _NSTRING_INLINES_H_
-
- #include <string.h>
-
- #if NSTRING_DEBUG > 0
- #include <stdio.h>
- #endif
-
- //__________________________________ PRIVATE INLINES __________________________________
-
- inline NString::strbody::strbody()
- {
- refs = 1;
- len = 0;
- bufsize = 0;
- str = NULL;
- }
-
- //__________________________________ PUBLIC INLINES __________________________________
-
- // *** Constructors & Destructors ***
-
-
- inline NString::NString (const NString& s)
- {
- s.sb->refs++;
- sb = s.sb;
-
- #if (NSTRING_DEBUG & 2) != 0
- printf("Construction of NString \"%s\" from another NString.\n", sb->str);
- #endif
- }
-
- //_________________________________________________________________________________
-
- // *** Accessing the NString's information ***
-
-
- inline char *NString::string(char *s) const
- {
- strcpy(s, sb->str);
- return s;
- }
-
- //_________________________________________________________________________________
-
- inline unsigned long int NString::length (void) const
- {
- return sb->len;
- }
-
- //_________________________________________________________________________________
-
- // *** Comparisons ***
-
-
- inline int NString::operator== (const char *s) const
- {
- return (strcmp(sb->str, s) == 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator!= (const char *s) const
- {
- return (strcmp(sb->str, s) != 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator>= (const char *s) const
- {
- return (strcmp(sb->str, s) >= 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator<= (const char *s) const
- {
- return (strcmp(sb->str, s) <= 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator> (const char *s) const
- {
- return (strcmp(sb->str, s) > 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator< (const char *s) const
- {
- return (strcmp(sb->str, s) < 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::compare (const char *s) const
- {
- return (strcmp(sb->str, s));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator== (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) == 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator!= (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) != 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator>= (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) >= 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator<= (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) <= 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator> (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) > 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator< (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str) < 0);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::compare (const NString& s) const
- {
- return (strcmp(sb->str, s.sb->str));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator== (const char c) const
- {
- return ((sb->len == 1) && (sb->str[0] == c));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator!= (const char c) const
- {
- return ((sb->len != 1) || (sb->str[0] != c));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator>= (const char c) const
- {
- return ((sb->len > 0) && (sb->str[0] >= c));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator<= (const char c) const
- {
- return ((sb->len == 0) || (sb->str[0] < c) || ((sb->len == 1) && (sb->str[0] == c)));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator> (const char c) const
- {
- return ((sb->str[0] > c) || ((sb->len > 1) && (sb->str[0] == c)));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::operator< (const char c) const
- {
- return ((sb->len == 0) || (sb->str[0] < c));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::compare (const char c) const
- {
- if ((sb->len == 0) || (sb->str[0] < c))
- return (-1);
- else if ((sb->len == 1) && (sb->str[0] == c))
- return (0);
- else
- return (1);
- }
-
- //_________________________________________________________________________________
-
- inline const NString& NString::min (const NString& other) const
- {
- return (*this < other ? *this : other);
- }
-
- //_________________________________________________________________________________
-
- inline const NString& NString::max (const NString& other) const
- {
- return (*this > other ? *this : other);
- }
-
- //_________________________________________________________________________________
-
- // *** Getting a substring ***
-
- inline NString NString::from (unsigned long int from) const
- {
- return (fromto(from, sb->len));
- }
-
- //_________________________________________________________________________________
-
- inline NString NString::to (unsigned long int to) const
- {
- return (fromto(0, to));
- }
-
- //_________________________________________________________________________________
-
- // *** Getting context position ***
-
- inline unsigned long int NString::leftpos(const NString& s) const
- {
- return (leftpos(s.sb->str));
- }
-
- //_________________________________________________________________________________
-
- inline unsigned long int NString::rightpos(const NString& s) const
- {
- return (rightpos(s.sb->str));
- }
-
- //_________________________________________________________________________________
-
- // *** Getting a context related substring ***
-
- inline char NString::leftocc (const Alphabet& a) const
- {
- unsigned long int pos = leftpos(a);
-
- return ((pos == 0) ? '\0' : sb->str[pos-1]);
- }
-
- //_________________________________________________________________________________
-
- inline char NString::rightocc (const Alphabet& a) const
- {
- unsigned long int pos = rightpos(a);
-
- return ((pos == 0) ? '\0' : sb->str[pos-1]);
- }
-
- //_________________________________________________________________________________
-
- // *** Context-related tests ***
-
- inline int NString::startswith (const char *context) const
- {
- return (strncmp(sb->str, context, strlen(context)) ? 0 : 1);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::startswith (const char context) const
- {
- return ((context != '\0') && (sb->str[0] == context));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::startswith (const NString& context) const
- {
- return (strncmp(sb->str, context.sb->str, context.sb->len) ? 0 : 1);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::startswith (const Alphabet& context) const
- {
- return ((sb->len != 0) && (context.contains(sb->str[0])));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::endswith (const char context) const
- {
- return ((sb->len != 0) && (sb->str[sb->len-1] == context));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::endswith (const NString& context) const
- {
- return (endswith(context.sb->str));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::endswith (const Alphabet& context) const
- {
- return ((sb->len != 0) && (context.contains(sb->str[sb->len - 1])));
- }
-
- //_________________________________________________________________________________
-
- inline int NString::contains (const char *context) const
- {
- return (strstr(sb->str, context) != NULL);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::contains (const char context) const
- {
- char *ctx = "x";
-
- ctx[0] = context;
- return (strstr(sb->str, ctx) != NULL);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::contains (const NString& context) const
- {
- return (strstr(sb->str, context.sb->str) != NULL);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::contains (const Alphabet& context) const
- {
- Alphabet a(sb->str);
-
- return (context <= a);
- }
-
- //_________________________________________________________________________________
-
- inline int NString::disjointed (const Alphabet& alpha) const
- {
- return ((alpha.card() == 0) || (leftpos(alpha) == 0));
- }
-
- //_________________________________________________________________________________
-
- // *** Input / Output ***
-
-
- inline istream& get (istream& theStream, NString& theString, const char terminator)
- {
- return get(theStream, theString, 0, terminator);
- }
-
- //_________________________________________________________________________________
-
- inline NString& NString::operator<< (const char *s)
- {
- return (*this += s);
- }
-
- //_________________________________________________________________________________
-
- inline NString& NString::operator<< (const NString& s)
- {
- return (*this += s);
- }
-
- //_________________________________________________________________________________
-
- inline NString& NString::operator<< (const char c)
- {
- return (*this += c);
- }
-
- //_________________________________________________________________________________
-
- inline NString& NString::operator<< (NString& (*manip)(NString&))
- {
- return manip(*this);
- }
-
- //_________________________________________________________________________________
-
- inline NString& endl (NString& s)
- {
- return (s += '\n');
- }
-
- //_________________________________________________________________________________
-
- inline NString& clear (NString& s)
- {
- return s.clear();
- }
-
- #endif
-